home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 176-200 / 179 / unixutil / entab.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  3KB  |  140 lines

  1. /* entab.c - compress spaces to tabs in a file.  If a single tabstop    *
  2.  *    is given, tabs are tabstop spaces apart, otherwise tabstops    *
  3.  *    are at <tab1>, <tab2>,..., <tabn>.  If no file is specified,    *
  4.  *    standard input is read and standard output written.        *
  5.  *                                    *
  6.  * entab [-<tab1> -<tab2> -<tabn>] [filename... ]            *
  7.  *                                    *
  8.  * entab (C) 1988 by Gary L. Brant                    *
  9.  *                                    *
  10.  * :ts=8                                */
  11.  
  12. #define MAXLINE 1000
  13. #include <stdio.h>
  14. char tabarray[MAXLINE];
  15.  
  16. main (argc, argv)    /* remove trailing blanks and tabs and */
  17.             /* compress blanks in source lines */
  18. int argc;
  19. char *argv[];
  20. {
  21.    int i = 0, j, k, l, ntabs = 0, tabstop = 8;
  22.    int argtabs[99];
  23.    char ch, *pch;
  24.    FILE *ifile, *fopen ();
  25.  
  26.    /* decode tabstop arguments */
  27.    while ((++i < argc) && (argv[i][0] == '-')) {
  28.       j = 1;
  29.       tabstop = 0;
  30.       while ((ch = argv[i][j++]) != '\0') {
  31.      if (ch >= '0' && ch <= '9') {
  32.         tabstop *= 10;
  33.         tabstop += ch - '0';
  34.      } else {
  35.         fputs ("bad args\n", stderr);
  36.         exit (20);
  37.      }
  38.       }
  39.       argtabs[ntabs++] = tabstop;
  40.    }
  41.  
  42.    /* fill tabarray with \1 at each tabstop position */
  43.    for (k = 0; k < MAXLINE; k++)
  44.       tabarray[k] = '\0';
  45.    if (ntabs > 1)
  46.       for (k = 0; k < ntabs; k++)
  47.      if ((l = argtabs[k]-1) < MAXLINE)
  48.         tabarray[l] = '\001';
  49.      else {
  50.         fputs ("bad tab specification\n", stderr);
  51.         exit (20);
  52.      }
  53.    else if ((tabstop > 0) && (tabstop < MAXLINE))
  54.       for (k = tabstop; k < MAXLINE; k += tabstop)
  55.      tabarray[k] = '\001';
  56.    else {
  57.       fputs ("bad tab specification\n", stderr);
  58.       exit (20);
  59.    }
  60.  
  61.    /* remaining arguments should be file names - entab them */
  62.    if (i < argc)
  63.       while (i < argc)
  64.      if ((ifile = fopen ((pch = argv[i++]), "r")) == NULL) {
  65.         fputs ("entab: cant open ", stderr);
  66.         fputs (pch, stderr);
  67.         putc ('\n', stderr);
  68.         exit (20);
  69.      } else
  70.         entab (ifile);
  71.    else
  72.       entab (stdin);
  73. }
  74.  
  75.  
  76. /* entab - insert tabs into one file */
  77.  
  78. entab (ifile)
  79. FILE *ifile;
  80. {
  81.    int n;
  82.    char inline[MAXLINE], outline[MAXLINE], *fgets ();
  83.  
  84.    while ((fgets (inline, MAXLINE, ifile)) != NULL) {
  85.       n = strlen (inline);
  86.       while (--n >= 0)            /* back over white space */
  87.      if (inline[n] != ' ' && inline[n] != '\t' && inline[n] != '\n')
  88.         break;
  89.       inline[n+1] = '\0';
  90.       compress (inline, outline, MAXLINE);
  91.       puts (outline);
  92.    }
  93. }
  94.  
  95.  
  96. /* compress - compress one line, replacing strings of blanks with tabs    *
  97.  * to tab stops specified on command line or default            */
  98.  
  99. compress (in, out, lim)
  100. char in[], out[];
  101. int  lim;
  102. {
  103.    register int i = 0, j = 0;
  104.    register char ch;
  105.  
  106.    while (((ch = in[i++]) != '\0') && (i < lim)) {
  107.       if (ch == ' ') {
  108.      register int k = i, tc;
  109.      while (((tc = tabarray[k]) == '\0') && (in[k] == ' ') && (k < lim))
  110.         k++;
  111.      if ((tc == '\001') && (k > i))
  112.         out[j++] = '\t';
  113.      else            /* avoid running through this again next trip */
  114.         while (i++ <= k)
  115.            out[j++] = ch;
  116.      i = k;
  117.       } else if (ch == '\042' || ch == '\047') {
  118.      register int tc;        /* avoid tabbing quoted strings */
  119.      out[j++] = ch;
  120.      while (((tc = in[i++]) != ch) && (i < lim)) {
  121.         if (tc == '\134') {     /* possible protected quote */
  122.            out[j++] = tc;
  123.            tc = in[i++];
  124.            if (i == lim) break;
  125.         }
  126.         out[j++] = tc;
  127.      }
  128.      out[j++] = ch;
  129.       } else
  130.      out[j++] = ch;
  131.    }
  132.    out[j] = ch;
  133. }
  134.  
  135.  
  136. void
  137. _wb_parse ()
  138. {
  139. }
  140.